<template>
  <div class="card-wrapper" :class="{ 'card-selected': selected }" @mouseover="mouseover" @mouseleave="mouseleave">
    <div class="template-card-container">
      <div class="image-container">
        <img
            :src="cardData.coverUrl"
            :alt="cardData.title"
            :title="cardData.title"
            class="template-image"
        />

        <!-- Enhanced mask layer -->
        <div ref="maskLayerRef" class="mask-layer">
          <div class="action-buttons">
            <button class="preview-button" @click="dialogVisible = true">
              <i class="fas fa-eye"></i>
              <span>预览模板</span>
            </button>
          </div>

          <!-- Decorative elements -->
          <div class="mask-decoration">
            <div class="decoration-dot"></div>
            <div class="decoration-dot"></div>
            <div class="decoration-dot"></div>
          </div>
        </div>

        <!-- Status badge -->
        <div class="status-badge">
          <i class="fas fa-star"></i>
          <span>推荐</span>
        </div>
      </div>

      <!-- Card content -->
      <div class="card-content">
        <div class="template-info">
          <h3 class="template-title">{{ cardData.title }}</h3>
          <div class="template-meta">
            <div class="meta-item">
              <i class="fas fa-user"></i>
              <span>专业模板</span>
            </div>
            <div class="meta-item action-bar">
              <button class="use-button" @click="preview">
                <i class="fas fa-plus"></i>
                <span>使用</span>
              </button>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Enhanced preview modal -->
  <PreviewImage v-show="dialogVisible" @close="close">
    <div class="preview-container">
      <img class="preview-image" :src="cardData.coverUrl" :alt="cardData.title" />
      <div class="preview-info">
        <h2>{{ cardData.title }}</h2>
        <p>高质量专业模板，适用于各种场景</p>
      </div>
    </div>
  </PreviewImage>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import PreviewImage from '@/components/PreviewImage/PreviewImage.vue';

const props = defineProps<{
  cardData: any;
}>();

const emit = defineEmits(['preview']);
const maskLayerRef = ref<any>(null);

// Smooth hover animations
const mouseover = () => {
  if (maskLayerRef.value) {
    maskLayerRef.value.style.opacity = '1';
    maskLayerRef.value.style.transform = 'translateY(0)';
  }
};

const mouseleave = () => {
  if (maskLayerRef.value) {
    maskLayerRef.value.style.opacity = '0';
    maskLayerRef.value.style.transform = 'translateY(10px)';
  }
};

// Preview functionality
const preview = () => {
  emit('preview', props.cardData);
};

const selected = ref(false);


defineExpose({ selected });

// Image preview modal
const dialogVisible = ref<boolean>(false);

const close = () => {
  dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css';

.card-wrapper {
  display: flex;
  flex-direction: column;
  width: 200px;
  height: 300px;
  margin: 0 20px 40px 20px;
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(145deg, #ffffff, #f8fafc);
  border: 1px solid rgba(100, 181, 246, 0.1);
  box-shadow: 0 4px 20px rgba(100, 181, 246, 0.08);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  cursor: pointer;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(145deg, rgba(100, 181, 246, 0.05), rgba(144, 202, 249, 0.02));
    opacity: 0;
    transition: opacity 0.4s ease;
    border-radius: 16px;
    z-index: 0;
  }

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 40px rgba(100, 181, 246, 0.15);
    border-color: rgba(100, 181, 246, 0.2);

    &::before {
      opacity: 1;
    }

    .template-card-container {
      .image-container .template-image {
        transform: scale(1.02);
      }

      .card-content {
        background: rgba(255, 255, 255, 0.95);
      }
    }

    .status-badge {
      transform: scale(1.05);
    }
  }

  &.card-selected {
    border: 2px solid #64b5f6;
    box-shadow: 0 8px 32px rgba(100, 181, 246, 0.25);

    &::before {
      opacity: 1;
      background: linear-gradient(145deg, rgba(100, 181, 246, 0.1), rgba(144, 202, 249, 0.05));
    }

    .status-badge {
      background: linear-gradient(135deg, #64b5f6, #42a5f5);
      color: white;
    }
  }
}

.template-card-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  z-index: 1;
}

.image-container {
  position: relative;
  height: 3000px;
  overflow: hidden;
  border-radius: 16px 16px 0 0;
  background: linear-gradient(135deg, #f5f7fa, #c3cfe2);

  .template-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .mask-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.4));
    backdrop-filter: blur(2px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 2;

    .action-buttons {
      display: flex;
      flex-direction: column;
      gap: 12px;
      align-items: center;

      .preview-button {
        background: linear-gradient(135deg, #64b5f6, #42a5f5);
        border: none;
        border-radius: 25px;
        padding: 10px 20px;
        color: white;
        font-size: 0.85rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: 8px;
        box-shadow: 0 4px 15px rgba(100, 181, 246, 0.3);

        &:hover {
          background: linear-gradient(135deg, #42a5f5, #1976d2);
          transform: translateY(-2px);
          box-shadow: 0 6px 20px rgba(100, 181, 246, 0.4);
        }

        i {
          font-size: 0.9rem;
        }
      }

      .quick-preview-button {
        background: rgba(255, 255, 255, 0.2);
        border: 1px solid rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        width: 40px;
        height: 40px;
        color: white;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        backdrop-filter: blur(10px);

        &:hover {
          background: rgba(255, 255, 255, 0.3);
          transform: scale(1.1);
        }

        i {
          font-size: 0.9rem;
        }
      }
    }

    .mask-decoration {
      position: absolute;
      bottom: 15px;
      left: 50%;
      transform: translateX(-50%);
      display: flex;
      gap: 6px;

      .decoration-dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.6);
        animation: pulse 2s infinite;

        &:nth-child(2) {
          animation-delay: 0.3s;
        }

        &:nth-child(3) {
          animation-delay: 0.6s;
        }
      }
    }
  }

  .status-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(248, 250, 252, 0.8));
    backdrop-filter: blur(10px);
    border: 1px solid rgba(100, 181, 246, 0.2);
    border-radius: 20px;
    padding: 4px 10px;
    font-size: 0.7rem;
    font-weight: 600;
    color: #64b5f6;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.3s ease;
    z-index: 3;

    i {
      font-size: 0.6rem;
    }
  }
}

.card-content {
  flex: 1;
  padding: 4px 8px;
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 16px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .template-info {
    .template-title {
      font-size: 0.95rem;
      font-weight: 700;
      color: #2c3e50;
      margin: 0 0 8px 0;
      line-height: 1.3;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .template-meta {
      display: flex;
      justify-content: space-between;
      margin-bottom: 12px;

      .meta-item {
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: 0.7rem;
        color: #64b5f6;
        font-weight: 500;

        i {
          font-size: 0.65rem;
        }
      }
    }
  }

  .action-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .rating {
      display: flex;
      gap: 2px;

      i {
        font-size: 0.7rem;
        color: #e0e7ff;
        transition: color 0.2s ease;

        &.filled {
          color: #fbbf24;
        }
      }
    }

    .use-button {
      background: linear-gradient(135deg, #10b981, #059669);
      border: none;
      border-radius: 15px;
      padding: 6px 12px;
      color: white;
      font-size: 0.7rem;
      font-weight: 600;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      gap: 4px;

      &:hover {
        background: linear-gradient(135deg, #059669, #047857);
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
      }

      i {
        font-size: 0.6rem;
      }
    }
  }
}

.preview-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;

  .preview-image {
    max-height: 80vh;
    max-width: 90vw;
    object-fit: contain;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  }

  .preview-info {
    text-align: center;
    color: white;

    h2 {
      font-size: 1.5rem;
      font-weight: 700;
      margin: 0 0 8px 0;
    }

    p {
      font-size: 1rem;
      opacity: 0.8;
      margin: 0;
    }
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 0.6;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.2);
  }
}

// Responsive design
@media (max-width: 768px) {
  .card-wrapper {
    width: 180px;
    height: 280px;
    margin: 0 15px 30px 15px;
  }

  .image-container {
    height: 180px;
  }

  .card-content {
    padding: 12px;

    .template-info .template-title {
      font-size: 0.85rem;
    }

    .template-meta .meta-item {
      font-size: 0.65rem;
    }

    .action-bar {
      .rating i {
        font-size: 0.65rem;
      }

      .use-button {
        font-size: 0.65rem;
        padding: 5px 10px;
      }
    }
  }
}

@media (max-width: 480px) {
  .card-wrapper {
    width: 160px;
    height: 260px;
    margin: 0 10px 25px 10px;
  }

  .image-container {
    height: 160px;
  }

  .mask-layer .action-buttons {
    gap: 8px;

    .preview-button {
      padding: 8px 16px;
      font-size: 0.8rem;
    }

    .quick-preview-button {
      width: 35px;
      height: 35px;
    }
  }
}
</style>